import Vue from 'vue'
import Vuex from 'vuex'
import {getLogin} from "../api/api";
import {getshopCar} from "../api/api";
import {getaddShop} from "../api/api";
import {getdelShop} from "../api/api";
import {getuserInfo} from "../api/api";

import { message } from 'ant-design-vue';
import 'ant-design-vue/dist/antd.less';
import router from '../router/index'
Vue.use(Vuex)
Vuex.Store.prototype.$message = message;
Vuex.Store.prototype.$router = router;

export default new Vuex.Store({
  state: {
    //登录modal
    visible:false,
    //注册modal
    showRegis:false,
    //忘记密码modal
    showForget:false,
    //登录状态
    login:false,
    //用户信息
    userInfo:[],
    // 购物车信息
    shopCar:[]
  },
  mutations: {
    //改变登录modal是否显示
    change(state,n){
      console.log(this);
      state.visible = n;
    },
    //是否显示注册modal
    changeShowRegis(state,n){
      state.showRegis = n;
    },
    //是否显示忘记密码
    changeShowForget(state,n){
      state.showForget = n;
    },
    //改变登录状态
    changeLogin(state,n){
      state.login = n;
    },
    // 改变个人性息
    changeUserInfo(state,n){
      state.userInfo = n;
    },
    //改变购物车信息
    changeShopCar(state,n){
      state.shopCar = n;
    }
  },
  actions: {
    // 是否登录 调用之后的接口
    IsLogin({commit},payload){
      getLogin(payload.username,payload.password).then(res=>{
        // console.log(res);
        //判断是否成功
        if (res.code==500){
          //改变登录状态
          commit("changeLogin",false);
          // 提示失败
          this.$message.error("登录失败");
        }else {
          //改变个人信息
          commit("changeUserInfo", res.userInfo);
          //改变登录状态
          commit("changeLogin", true);
          // 提示成功
          this.$message.success("登录成功");
          //登录modal消失
          commit("change",false);
          //获取购物车信息
          getshopCar(1).then(res => {
            //改变购物车信息
            commit("changeShopCar", res.shoppingCartList);
          })
        }
      }).catch(()=>{
          commit("changeLogin",false);
        this.$message.error("登录失败");
      });

    },
    //添加购物策划
    addshopCar({commit},courseid){
      //添加购物车接口
        getaddShop(courseid).then(res=>{
          console.log(res);
          //成功
          if(res.code==0){
            //提示成功
            this.$message.success(res.msg);
            //调用购物车接口
            getshopCar(1).then(res => {
              //更改信息
              commit("changeShopCar", res.shoppingCartList);
            })
          }else {
            //提示失败
            this.$message.error("添加失败");
          }

        })
    },
    //删除购物车
    delshopCar({commit},shopid){
      //请求删除购物车的接口
      getdelShop(shopid).then(res=>{
        //成功之后
        if(res.code == 0){
          // 请求购物数据
          getshopCar(1).then(res => {
            commit("changeShopCar", res.shoppingCartList);
          })
        }else {
          this.$message.error("错误");
        }

      })
    },
    //个人信息
    getLoginsta({commit}){
      getuserInfo().then(res=>{
        // console.log(res);
        if(res.code==0){
          // 更改登录状态
          commit("changeLogin",true);
          //重获个人信息
          commit("changeUserInfo",res.userInfo);
          // 重新获取购物车信息
          getshopCar(1).then(res => {
            commit("changeShopCar", res.shoppingCartList);
          })
        }else {
          commit("changeLogin",false);
        }
      })
    }
  },
  getters:{
    //返回购物车信息的长度
    shopCarlength(state){
      return state.shopCar.length;
    }
  },
  modules: {
  }
})
